package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.ServerResponse;
import com.chixing.mapper.CompanyDetailMapper;
import com.chixing.mapper.CompanyManageMapper;
import com.chixing.mapper.JobDetailMapper;
import com.chixing.pojo.*;
import com.chixing.service.ICompanyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司表 服务实现类
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Service
public class CompanyDetailServiceImpl implements ICompanyDetailService {

    @Autowired(required = false)
    private CompanyDetailMapper companyDetailMapper;

    @Autowired
    private JobDetailMapper jobDetailMapper;

    @Autowired
    private CompanyManageMapper companyManageMapper;

    @Override
    public ServerResponse getById(Integer companyId) {
        CompanyDetail companyDetail = companyDetailMapper.selectById(companyId);
        if (companyDetail == null) {
            return ServerResponse.fail("查询失败",null);
        }else {
            return ServerResponse.success("查询成功",companyDetail);
        }
    }

    @Override
    public ServerResponse save(CompanyDetail companyDetail) {
        int rows = companyDetailMapper.insert(companyDetail);
        if (rows <= 0) {
            return ServerResponse.fail("插入失败",null);
        }else {
            return ServerResponse.success("插入成功",companyDetail);
        }
    }

    @Override
    public ServerResponse update(CompanyDetail companyDetail) {
        int rows = companyDetailMapper.updateById(companyDetail);
        if (rows <= 0) {
            return ServerResponse.fail("更新失败", null);
        } else {
            return ServerResponse.success("更新成功", companyDetail);
        }
    }

    @Override
    public ServerResponse delete(Integer companyId) {
        int rows = companyDetailMapper.deleteById(companyId);
        if (rows <= 0) {
            return ServerResponse.fail("删除失败", null);
        } else {
            return ServerResponse.success("删除成功", rows);
        }
    }

    @Override
    public ServerResponse getAll() {
        List<CompanyDetail> companyDetailList = companyDetailMapper.selectList(null);
        if (companyDetailList.size()!= 0) {
            return ServerResponse.success("查询成功",companyDetailList);
        }else {
            return ServerResponse.fail("查询失败",null);
        }
    }

    /**
     * 公司首页，条件分页查询
     * @param companyQuery
     * @return
     */
    @Override
    public ServerResponse getAllCompany(CompanyQuery companyQuery) {
        QueryWrapper<CompanyDetail> queryWrapper = new QueryWrapper<>();
        if(!companyQuery.getCompanyFinance().equals("")){
            queryWrapper.eq("company_finance",companyQuery.getCompanyFinance());
        }
        if(!companyQuery.getCompanyScale().equals("")){
            queryWrapper.eq("company_scale",companyQuery.getCompanyScale());
        }
        if(!companyQuery.getCompanyIndustry().equals("")){
            if(!companyQuery.getCompanyIndustry().equals("不限"))
                queryWrapper.eq("company_industry",companyQuery.getCompanyIndustry());
        }
        List<CompanyDetail> companyDetails = companyDetailMapper.selectList(queryWrapper);
        List<CompanyAndJobs> companyAndJobsList = new ArrayList<>();
        for (CompanyDetail companyDetail:companyDetails) {
            CompanyAndJobs companyAndJobs1 = new CompanyAndJobs();
            QueryWrapper<JobDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("company_id",companyDetail.getCompanyId());

            List<JobDetail> jobDetails = jobDetailMapper.selectList(queryWrapper1);
            companyAndJobs1.setJobDetailList(jobDetails);
            companyAndJobs1.setCompanyDetail(companyDetail);
            companyAndJobsList.add(companyAndJobs1);
        }
        if (companyAndJobsList !=null){
            return ServerResponse.success("成功",companyAndJobsList);
        }
        return ServerResponse.fail("失败",null);
    }

    @Override
    public CompanyAndJobs getCompanyAndJobs(Integer companyId) {
        CompanyDetail companyDetail = companyDetailMapper.selectById(companyId);
        CompanyAndJobs companyAndJobs = new CompanyAndJobs();
        companyAndJobs.setCompanyDetail(companyDetail);
        QueryWrapper<JobDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyDetail.getCompanyId());
        List<JobDetail> jobDetails = jobDetailMapper.selectList(queryWrapper);
        companyAndJobs.setJobDetailList(jobDetails);
        return companyAndJobs;
    }

    /**
     * 页面首页，公司工作信息展示
     */
    @Override
    public ServerResponse getCompany() {
        PageHelper.startPage(1,6);
        List<CompanyDetail> companyDetails = companyDetailMapper.selectList(null);
        PageInfo<CompanyDetail> pageInfo = new PageInfo<>(companyDetails);
        List<CompanyAndJobs> companyAndJobsList = new ArrayList<>();
        for (CompanyDetail companyDetail:companyDetails) {
            CompanyAndJobs companyAndJobs1 = new CompanyAndJobs();
            QueryWrapper<JobDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("company_id",companyDetail.getCompanyId());
            PageHelper.startPage(1,3);
            List<JobDetail> jobDetails = jobDetailMapper.selectList(queryWrapper1);
            companyAndJobs1.setJobDetailList(jobDetails);
            companyAndJobs1.setCompanyDetail(companyDetail);
            companyAndJobsList.add(companyAndJobs1);
        }
        if (companyAndJobsList !=null){
            PageInfo<CompanyAndJobs> pageInfo1 = new PageInfo<>(companyAndJobsList);
            pageInfo1.setPageNum(pageInfo.getPageNum());
            companyAndJobsList.forEach(System.out::println);
            return ServerResponse.success("成功",pageInfo1);
        }
        return ServerResponse.fail("失败",null);
    }

    @Override
    public Integer getCompanyManages(Integer companyId) {
        QueryWrapper<CompanyManage> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);
        List<CompanyManage> companyManages = companyManageMapper.selectList(wrapper);
        return companyManages.size();
    }

    @Override
    public List<JobAndManage> getJobAndManage(Integer companyId) {
        QueryWrapper<JobDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);
        List<JobDetail> jobDetails = jobDetailMapper.selectList(wrapper);
        List<JobAndManage> jobAndManages = new ArrayList<>();
        for (JobDetail jobDetail:jobDetails) {
            CompanyManage companyManage = companyManageMapper.selectById(jobDetail.getManageId());
            JobAndManage jobAndManage = new JobAndManage(jobDetail,companyManage);
            jobAndManages.add(jobAndManage);
        }
        return jobAndManages;
    }


    //后台模块

    @Override
    public CompanyDetail selectById(Integer id) {
        CompanyDetail companyDetail = companyDetailMapper.selectById(id);
        return companyDetail;
    }

    @Override
    public CompanyDetail selectByName(String companyName) {
        QueryWrapper<CompanyDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("company_name",companyName);
        CompanyDetail companyDetails = companyDetailMapper.selectOne(wrapper);
        return companyDetails;
    }

    @Override
    public boolean insertCompanyDetail(CompanyDetail companyDetail) {
        int insert = companyDetailMapper.insert(companyDetail);
        return insert>0;
    }

    @Override
    public boolean updateCompanyDetail(CompanyDetail companyDetail) {
        int i = companyDetailMapper.updateById(companyDetail);
        return i>0;
    }


}
